package com.iterator.service.web.marketManager;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iterator.bean.web.pojo.dhy.Keyword;
import com.iterator.bean.web.dhy.*;
import com.iterator.bean.web.pojo.dhy.KeywordExample;
import com.iterator.bean.web.pojo.dhy.MallRegion;
import com.iterator.bean.web.pojo.dhy.MallRegionExample;
import com.iterator.mapper.web.marketManager.KeywordMapper;
import com.iterator.mapper.web.marketManager.MallRegionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: DHY
 * @Date: 2021/1/9 16:27
 * @Description: 商场管理
 */
@Service
public class MallManageServiceImpl implements MallManageService {
    @Autowired
    KeywordMapper keywordMapper;
    @Autowired
    MallRegionMapper mallRegionMapper;

    /**
     * 关键词页面显示,dhy
     *
     * @param listBO
     * @return
     */
    @Override
    public KeywordListVO showList(KeywordListBO listBO) {
        PageHelper.startPage(Integer.parseInt(listBO.getPage()), Integer.parseInt(listBO.getLimit()));
        KeywordExample keywordExample = new KeywordExample();
        //分页
        keywordExample.setOrderByClause(listBO.getSort() + " " + listBO.getOrder());
        KeywordExample.Criteria criteria = keywordExample.createCriteria();
        //关键字
        if (listBO.getKeyword() != null&&listBO.getKeyword().length()!=0) {
            //忽略输入的空格
            String keyWord = listBO.getKeyword().replaceAll(" ", "");
            criteria.andKeywordLike("%" + keyWord + "%");
        }
        //url
        if (listBO.getUrl() != null&&listBO.getUrl().length()!=0) {
            //忽略输入的空格
            String url = listBO.getUrl().replaceAll(" ", "");
            criteria.andUrlLike("%" + url + "%");
        }
        //查询所有信息
        List<Keyword> keywords = keywordMapper.selectByExample(keywordExample);
        PageInfo<Keyword> keywordPageInfo = new PageInfo<>(keywords);
        //查询出来的条数
        int total = (int) keywordPageInfo.getTotal();
        //封装
        KeywordListVO listVO = new KeywordListVO();
        listVO.setTotal(total);
        listVO.setItems(keywords);
        return listVO;
    }

    /**
     * 创建关键字,dhy
     * @param createBO
     * @return
     */
    @Override
    public KeywordCreateVO keywordCreate(KeywordCreateBO createBO) {
        Keyword keyword = new Keyword(null,createBO.getKeyword(),
                createBO.getUrl(),createBO.getIsHot(),createBO.getIsDefault(),
                100,new Date(),new Date(),false);

        //插入数据
        keywordMapper.insertInfo(keyword);
        //获取返回参数
        KeywordCreateVO keywordCreateVO= keywordMapper.selectCreateInfoById(keyword.getId());
        return keywordCreateVO;
    }

    /**
     * 删除关键字,dhy
     * @param id
     */
    @Override
    public void keywordDelete(Integer id) {
        keywordMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改关键字
     * @param keyword
     */
    @Override
    public void keywordUpdate(Keyword keyword) {
        keyword.setUpdateTime(new Date());

        keywordMapper.updateByPrimaryKey(keyword);
    }


    /**
     * 行政区域显示
     * @return
     */
    @Override
    public List<RegionListVO> regionList() {
        List<RegionListVO> regionListVOS = new ArrayList<RegionListVO>();
        //查询省份信息
        List<MallRegion> mallRegions = getMallRegions(0);
        for (MallRegion mallRegion : mallRegions) {
            RegionListVO regionListVO = new RegionListVO();
            regionListVO.setId(mallRegion.getId());
            regionListVO.setName(mallRegion.getName());
            regionListVO.setType(mallRegion.getType());
            regionListVO.setCode(mallRegion.getCode());

            //查询市
            List<MallRegion> mallRegions1 = getMallRegions(mallRegion.getId());
          List<RegionListVO.ChildrenBeanX> childrenBeanXES = new ArrayList<>();
            for (MallRegion region : mallRegions1) {
                RegionListVO.ChildrenBeanX childrenBeanX = new RegionListVO.ChildrenBeanX();
                childrenBeanX.setId(region.getId());
                childrenBeanX.setName(region.getName());
                childrenBeanX.setType(region.getType());
                childrenBeanX.setCode(region.getCode());

                //查询县
                List<MallRegion> mallRegions2 = getMallRegions(region.getId());
               List<RegionListVO.ChildrenBeanX.ChildrenBean> childrenBeans = new ArrayList<>();
                for (MallRegion mallRegion1 : mallRegions2) {
                    RegionListVO.ChildrenBeanX.ChildrenBean childrenBean = new RegionListVO.ChildrenBeanX.ChildrenBean();
                    childrenBean.setId(mallRegion1.getId());
                    childrenBean.setName(mallRegion1.getName());
                    childrenBean.setType(mallRegion1.getType());
                    childrenBean.setCode(mallRegion1.getCode());
                    //封装县的list
                    childrenBeans.add(childrenBean);
                }
                //封装县
                childrenBeanX.setChildren(childrenBeans);
                //封装市list
                childrenBeanXES.add(childrenBeanX);
            }
            //封装市
            regionListVO.setChildren(childrenBeanXES);
            //封装省list
            regionListVOS.add(regionListVO);
        }

        return regionListVOS;
    }

    /**
     * 根据pid查询信息
     * @param id
     * @return
     */
    private List<MallRegion> getMallRegions( Integer id) {
        MallRegionExample mallRegionExample = new MallRegionExample();
        MallRegionExample.Criteria criteria = mallRegionExample.createCriteria();
        criteria.andPidEqualTo(id);
        return mallRegionMapper.selectByExample(mallRegionExample);
    }
}
